package com.zy.utils

import android.graphics.Canvas
import android.graphics.Paint
import com.google.gson.Gson
import com.zy.utils.data.Point
import com.zy.utils.data.PointLocalData
import com.zy.zlog.ZLog.d


//画布工具
object CanvasUtils {

    //分组绘制

    @JvmStatic
    fun drawTextCenter(
        canvas: Canvas,
        paint: Paint,
        centerX: Float,
        centerY: Float,
        text: String?
    ) {
        //计算基线坐标
        val baseLineY = getBaseLineY(paint, centerY)
        canvas.drawText(text!!, centerX, baseLineY, paint)
    }

    private fun getBaseLineY(textPaint: Paint, centerY: Float): Float {
        val fontMetrics = textPaint.fontMetrics
        return centerY + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent
    }

    //绘制横竖辅助线
    @JvmStatic
    fun drawAssistLine(canvas: Canvas, assistLinePaint: Paint?, d: Int) {
        val w = canvas.width
        val h = canvas.height
        var baseX = d
        var baseY = d
        while (baseX < w) {
            canvas.drawLine(baseX.toFloat(), 0f, baseX.toFloat(), h.toFloat(), assistLinePaint!!)
            baseX += d
        }
        while (baseY < h) {
            canvas.drawLine(0f, baseY.toFloat(), w.toFloat(), baseY.toFloat(), assistLinePaint!!)
            baseY += d
        }
    }

    //绘制横竖辅助线
    @JvmStatic
    fun drawAssistLineV2(canvas: Canvas, assistLinePaint: Paint?, d: Int) {
        val w = canvas.width
        val h = canvas.height
        val wSize = w / d
        val wAss = wSize / 2 * d + d / 2
        canvas.drawLine(wAss.toFloat(), 0f, wAss.toFloat(), h.toFloat(), assistLinePaint!!)
        val hSize = h / d
        val hAss = hSize / 2 * d + d / 2
        canvas.drawLine(0f, hAss.toFloat(), w.toFloat(), hAss.toFloat(), assistLinePaint)
    }

    fun drawAssistLineY(canvas: Canvas, assistLinePaint: Paint?, d: Int) {
        val w = canvas.width
        val h = canvas.height
        var baseY = d
        while (baseY < h) {
            canvas.drawLine(0f, baseY.toFloat(), w.toFloat(), baseY.toFloat(), assistLinePaint!!)
            baseY += d
        }
    }

    fun drawAssistLineX(canvas: Canvas, assistLinePaint: Paint?, d: Int) {
        val w = canvas.width
        val h = canvas.height
        var baseX = d
        while (baseX < w) {
            canvas.drawLine(baseX.toFloat(), 0f, baseX.toFloat(), h.toFloat(), assistLinePaint!!)
            baseX += d
        }
    }

    /**
     * @param baseX
     * @param baseY
     * @param d     间距
     * @param array 数组
     * @return
     */
    @JvmStatic
    fun buildFirst(
        baseX: Int,
        baseY: Int,
        d: Int,
        array: IntArray,
        per: Int,
        type: Point.Type
    ): List<Point> {
        val list = ArrayList<Point>()
        for (i in array.indices) {
            val point = Point.Builder().setOriginX(baseX + i)
                .setOriginY(baseY)
                .setV(array[i].toString())
                .setPer(per.toFloat())
                .setD(d)
                .setIndex(i)
                .build(type)
            list.add(point)
            //            if (type == 4) {
//
//            } else {
//                list.add(new Point(baseX + d * i, baseY, String.valueOf(array[i]), per, i));
//            }
        }
        return list
    }

    fun buildFirst(
        baseX: Int,
        baseY: Int,
        d: Int,
        array: IntArray,
        per: Int,
        circle: Boolean
    ): List<Point> {
        val list = ArrayList<Point>()
        for (i in array.indices) {
            val point = Point.Builder().setOriginX(baseX + i)
                .setOriginY(baseY)
                .setV(array[i].toString())
                .setPer(per.toFloat())
                .setD(d)
                .setIndex(i)
                .setCircle(circle)
                .build(Point.Type.DEFAULT)
            list.add(point)
            //            if (type == 4) {
//
//            } else {
//                list.add(new Point(baseX + d * i, baseY, String.valueOf(array[i]), per, i));
//            }
        }
        return list
    }

    fun buildAssistList(
        baseX: Int,
        baseY: Int,
        d: Int,
        array: IntArray,
        per: Int,
        type: Point.Type
    ): List<Point> {
        val list = ArrayList<Point>()
        for (i in array.indices) {
            val point = Point.Builder().setOriginX(baseX + i)
                .setOriginY(baseY)
                .setV(array[i].toString())
                .setPer(per.toFloat())
                .setD(d)
                .setCanMove(false)
                .setIndex(i)
                .build(type)
            list.add(point)
            //            if (type == 1) {
//                list.add(new AssistPoint(baseX + d * i, baseY, String.valueOf(array[i]), per));
//            }
        }
        return list
    }

    fun buildPointStr(list: List<Point>): String {
        return Gson().toJson(PointLocalData(list))
    }

    fun buildPointList(str: String): List<Point> {
        val oldList = Gson().fromJson(str, PointLocalData::class.java).list
        val newList = mutableListOf<Point>()
        oldList.forEachIndexed { _, point ->
            newList.add(point.toBuilder().build())
        }
        return newList
    }
}